package org.isima.ui.faces.bean.documents;

import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.isima.ui.faces.model.File;
import org.isima.ui.faces.model.FileNode;
import org.isima.ui.faces.service.FileService;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.model.TreeNode;

/**
 * Detail des fichiers contenus dans un repertoire
 */
@ManagedBean
@ApplicationScoped
public class FilesListBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private List<File> filesList;
	private String dirName;
	private String dirPath;
	private File currentFile;
	private TreeNode selectedNode;
	private FileService service;

	public List<File> getFilesList() {
		return filesList;
	}

	public void setFilesList(List<File> filesList) {
		this.filesList = filesList;
	}

	public FilesListBean() throws InstantiationException, IllegalAccessException
	{
		service = new FileService();
		filesList=service.doFindAll(null);
		currentFile=new File();
	}
	
	/**
	 * Selection d'un noeud dans l'arbre des repertoires
	 * @param event Selection d'un noeud
	 */
	public void onNodeSelect(NodeSelectEvent event)
	{
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", event.getTreeNode().toString());
		
        FacesContext.getCurrentInstance().addMessage(null, message);  
		dirPath = ((FileNode)event.getTreeNode().getData()).getFile().getAbsolutePath();
		filesList = service.doFindAll(event.getTreeNode());
		dirName = ((FileNode)event.getTreeNode().getData()).getFile().getName();
		selectedNode = event.getTreeNode();
	}

	public String getDirName() {
		return dirName;
	}

	public void setDirName(String dirName) {
		this.dirName = dirName;
	}

	public String getDirPath() {
		return dirPath;
	}

	public void setDirPath(String dirPath) {
		this.dirPath = dirPath;
	}

	public File getCurrentFile() {
		return currentFile;
	}

	public void setCurrentFile(File currentFile) {
		this.currentFile = currentFile;
	}
	
	public void save()
	{
		service.create(currentFile.getName(), dirPath);
		filesList = service.doFindAll(selectedNode);
	}
}
